/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.service;

import java.util.List;
import java.util.Map;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.storage.common.Constant;
import com.jeesite.modules.storage.dao.AddressDao;
import com.jeesite.modules.storage.dao.BrandDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.storage.entity.FreightTpl;
import com.jeesite.modules.storage.dao.FreightTplDao;

/**
 * 运费模板Service
 * @author 吴争辉
 * @version 2020-02-01
 */
@Service
@Transactional(readOnly=true)
public class FreightTplService extends CrudService<FreightTplDao, FreightTpl> {

	@Autowired
	private AddressDao addressDao;

	@Autowired
	private BrandDao brandDao;

	/**
	 * 获取单条数据
	 * @param freightTpl
	 * @return
	 */
	@Override
	public FreightTpl get(FreightTpl freightTpl) {
		return super.get(freightTpl);
	}

	/**
	 * 查询分页数据
	 * @param freightTpl 查询条件
	 * @param freightTpl.page 分页对象
	 * @return
	 */
	@Override
	public Page<FreightTpl> findPage(FreightTpl freightTpl) {
		return super.findPage(freightTpl);
	}

	/**
	 * 保存数据（插入或更新）
	 * @param freightTpl
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(FreightTpl freightTpl) {
		if(StringUtils.isBlank(freightTpl.getBrandId())){
			freightTpl.setBrandId(Constant.DEFAULT_ID);
		}
		super.save(freightTpl);
	}

	/**
	 * 更新状态
	 * @param freightTpl
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(FreightTpl freightTpl) {
		super.updateStatus(freightTpl);
	}

	/**
	 * 删除数据
	 * @param freightTpl
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(FreightTpl freightTpl) {
		super.delete(freightTpl);
	}

	/**
	 * 获取Id【品牌、发货地以及收货地】
	 * @param brandId 品牌Id
	 * @param from 发货地
	 * @param to 收货地
	 * @return 查找Id
	 */
	public String getId(String brandId, String from, String to){
		return dao.findId(brandId,from,to);
	}

	/**
	 * 获取运费
	 * @param num 数量
	 * @param brandId 品牌Id
	 * @param receive 收件地址
	 * @return
	 */
	public Double getFreight(Integer num,String brandId, String receive)
	{
		String store = brandDao.findStoreById(brandId);
		List<Map> brandMaps = dao.findBrandList(brandId,store,receive);
		if(brandMaps.size() > 0){
			return  Double.parseDouble(String.valueOf(brandMaps.get(0).get("freight")))* num;
		}
		List<Map> sysMaps = dao.findSysList(store,receive);
		if(sysMaps.size() > 0){
			return  Double.parseDouble(String.valueOf(sysMaps.get(0).get("freight")))* num;
		}
		return 0d;
	}
}